package com.lhd.qd.module.system.role.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhd.qd.common.base.ApiResult;
import com.lhd.qd.common.base.BaseController;
import com.lhd.qd.common.base.BasePageQuery;
import com.lhd.qd.module.system.role.model.dto.RoleSaveDTO;
import com.lhd.qd.module.system.role.model.vo.RoleDetailVO;
import com.lhd.qd.module.system.role.model.vo.RoleListVO;
import com.lhd.qd.module.system.role.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "角色")
@RequestMapping("role")
@RestController
public class RoleController extends BaseController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "分页列表", response = RoleListVO.class)
    @GetMapping
    public ResponseEntity<ApiResult<IPage<RoleListVO>>> pageRole(BasePageQuery query) {

        return success(roleService.pageRole(query));
    }

    @ApiOperation(value = "详情", response = RoleDetailVO.class)
    @GetMapping("{id}")
    public ResponseEntity<ApiResult<RoleDetailVO>> getRoleById(@PathVariable("id") Long id) {

        return success(roleService.getRoleById(id));
    }

    @ApiOperation(value = "新增")
    @PostMapping
    public ResponseEntity<ApiResult> saveRole(@Validated @RequestBody RoleSaveDTO saveDTO) {

        return successOrFail(roleService.saveRole(saveDTO));
    }

    @ApiOperation(value = "修改")
    @PutMapping("{id}")
    public ResponseEntity<ApiResult> updateRoleById(@PathVariable("id") Long id, @Validated @RequestBody RoleSaveDTO saveDTO) {

        return successOrFail(roleService.updateRoleById(id, saveDTO));
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("{id}")
    public ResponseEntity<ApiResult> removeRoleById(@PathVariable("id") Long id) {

        return successOrFail(roleService.removeRoleById(id));
    }
}
